package test.patterns;

/** A simple demonstration of the Observer pattern.
    A cell in a spread sheet is monitored by two observers.

   This source code is from the book 
     "Flexible, Reliable Software:
       Using Patterns and Agile Development"
     published 2010 by CRC Press.
   Author: 
     Henrik B Christensen 
     Computer Science Department
     Aarhus University

   This source code is provided WITHOUT ANY WARRANTY either 
   expressed or implied. You may study, use, modify, and 
   distribute it for non-commercial purposes. For any 
   commercial use, see http://www.baerbak.com/
 */

public class ObsDemoObserver {
	public static void main(String[] args) {
		ObsSpreadsheetCell a1 = new ObsSpreadsheetCell();
		ObsObserver 
		observer1 = new PieChartWindow(a1),
		observer2 = new BarChartWindow(a1);
		a1.addObserver(observer1); 
		a1.addObserver(observer2);

		a1.changeValue(32);
		a1.changeValue(42);

		a1.removeObserver(observer1);
		a1.changeValue(12);
	}
}

class PieChartWindow implements ObsObserver {
	ObsSpreadsheetCell myCell;
	public PieChartWindow(ObsSpreadsheetCell c){
		myCell = c;
	}
	public void update() {
		System.out.println( "Pie chart notified: value: "+
				myCell.getValue() );
	}
}

class BarChartWindow implements ObsObserver {
	ObsSpreadsheetCell myCell;
	public BarChartWindow(ObsSpreadsheetCell c){
		myCell = c;
	}
	public void update() {
		System.out.println( "Value "+ myCell.getValue()+
				" in Bar chart" );
	}
}